package com.wsg.lightaccounting.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wsg.lightaccounting.Model.AccountManager;
import com.wsg.lightaccounting.Model.CashCard;
import com.wsg.lightaccounting.Model.NetworkPayment;
import com.wsg.lightaccounting.PopupWindow.EditPopupWindow;
import com.wsg.lightaccounting.R;

public class NetworkPaymentActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView back,check;
    private LinearLayout name,type;
    private TextView name_text,type_text;
    private boolean isEdit = false;
    private long id = -1L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = this.getIntent().getExtras();
        isEdit = bundle.getBoolean("isEdit");
        setContentView(R.layout.activity_network_payment);
        intiView();
    }
    private void intiView(){
        back = (ImageView) findViewById(R.id.back);
        check = (ImageView) findViewById(R.id.check);


        type = (LinearLayout) findViewById(R.id.type_choose);
        name = (LinearLayout) findViewById(R.id.name_add);

        type_text = (TextView) findViewById(R.id.type_name);
        name_text = (TextView) findViewById(R.id.name_wallet);

        back.setOnClickListener(this);
        check.setOnClickListener(this);

        type.setOnClickListener(this);
        name.setOnClickListener(this);

        if (isEdit){
            Bundle bundle = this.getIntent().getExtras();
            type_text.setText(bundle.getString("type"));
            name_text.setText(bundle.getString("name"));
            id = bundle.getLong("id");
            TextView textView = (TextView) findViewById(R.id.title);
            textView.setText("编辑网络支付");
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.check:
                enter();
                MainActivity.updateAllUI(this);
                finish();
                break;
            case R.id.type_choose:
                showEditPopupWinddow(type_text, EditPopupWindow.INPUT_LIMIT_NONE);
                break;
            case R.id.name_add:
                showEditPopupWinddow(name_text,EditPopupWindow.INPUT_LIMIT_NONE);
                break;
            default:
                break;
        }

    }
    private void showEditPopupWinddow(TextView textView,int i){
        EditPopupWindow editPopupWindow = new EditPopupWindow(this,this.findViewById(R.id.mark_size).getWidth(),
                this.findViewById(R.id.mark_size).getHeight(),textView,i);
        editPopupWindow.showAtLocation(this.findViewById(R.id.mark_size), Gravity.BOTTOM,0,0);
    }

    private void enter(){
        String name = String.valueOf(name_text.getText());
        String type = String.valueOf(type_text.getText());
        if (!isEdit) {
            AccountManager.getAccountManager(this).addNetworkPayment(new NetworkPayment(0L,name,type,0));
        }
        else{
            AccountManager.getAccountManager(this).updateNetworkPayment(id,name,type);
        }
    }


}
